import { defineStore } from 'pinia'
import type { SongListType, StateType, collectSongListType } from '../types'
import { getSongUrlData, getSongIsPlayData, getLyricsData } from '../api/api'
import { Toast } from 'vant';

export const usePlayerStore = defineStore('player', {
    state: (): StateType => {
        return {
            isShow: false,
            isPlay: false,
            songUrl: '',
            songList: [],
            songLyric: [],
            collectMusic: [],
            collectMusicList: [],
            currentIndex: -1,

        }
    },
    getters: {
        // 获取当前歌曲信息详情
        songObj(state) {
            let lastSong = state.songList[0];
            return lastSong
        },

    },

    actions: {
        // 点击榜单歌曲列表播放音乐
        changeBallStateActions(songItem: SongListType) {
            this.isShow = true;
            let songObj = this.songList.find(item => item.id === songItem.id)
            if (songObj) {
                this.songList.unshift(songObj);

            } else {
                this.songList.unshift(songItem);

            }
            this.isPlay = true;
            this.currentIndex = songItem.id
            this.songList = [...new Set(this.songList)]
            this.isPlayMusic(songItem.id)
            this.getSongLyric(songItem.id)
        },

        // 获取全部歌曲列表
        getAllSongActions(songs: Array<SongListType>) {

            this.isShow = true;
            if (this.songList.length) {
                // this.songList = Array.from(new Set(this.songList))
                this.songList = [...this.songList,...songs]
            } else {

                this.songList = songs

            }
            this.songList = [...new Set(this.songList)]
            Toast.success('添加成功');

        },

        // 添加收藏喜欢的音乐 
        addLikeMusicActions(songItem: SongListType) {

            this.collectMusic.unshift(songItem)

        },
      
        // 删除收藏喜欢的音乐 
        delLikeMusicActions(songItem: SongListType) {
            this.collectMusic.map((item, index) => {
                if (songItem.id == item.id) {

                    this.collectMusic.splice(index, 1)

                }
            })

            console.log(this.collectMusic);

        },
          // 添加收藏喜欢的歌单 
          addLikeSongListActions(collectSongItem: collectSongListType) {

            this.collectMusicList.unshift(collectSongItem)

            console.log(this.collectMusicList);

        },
        
        // 删除收藏喜欢的歌单 
        delLikeSongListActions(collectSongItem: collectSongListType) {
            this.collectMusicList.map((item, index) => {
                if (collectSongItem.id == item.id) {

                    this.collectMusicList.splice(index, 1)

                }
            })

            console.log(this.collectMusicList);

        },
        // 获取可播放歌曲Url
        async isPlayMusic(id: number) {
            // 判断歌曲是否可用
            let { data } = await getSongIsPlayData(id)
            if (data.success) {
                // 根据歌曲id获取歌曲url
                getSongUrlData(id).then(res => {
                    this.songUrl = res.data.data[0].url
                })
            } else {
                Toast.fail('没有版权');
                return
            }
        },
        // 获取歌曲歌词
        async getSongLyric(id: number) {

            let { data } = await getLyricsData(id)
            let lrc = data.lrc.lyric
            let res = [];
            const lyrReg = /^\[(\d+):(\d+\.\d+)\](.*)/gim;
            while (lyrReg.test(lrc)) {
                let m = parseInt(RegExp.$1) * 60;
                let time = Math.ceil((m + parseFloat(RegExp.$2)) * 1000) / 1000;
                res.push({
                    time,
                    lyc: RegExp.$3,
                });
            }
            this.songLyric = res;
            // console.log(res);
            return res;

        },



    }


})

